package com.ews.onlineexchange.service;

import java.io.Serializable;
import java.math.BigDecimal;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;
import javax.persistence.Query;

import com.ews.onlineexchange.model.ProfessionRatingRecord;
import com.ews.onlineexchange.model.TradeRating;
import com.ews.onlineexchange.model.approved.AnnualReviewRecord;

@Named
@Stateless
public class ServiceSupplierScoreCalculator implements Serializable {
	@Inject
	EntityManager entityManager;
	private static final String GET_SUPPLIER_TRADERATING = ""+
			"SELECT tradeRating FROM TradeRating tradeRating"+
			" WHERE tradeRating.trade.supplier.id = :supplierId ";

	/**
	 * 新评价得分
	 * @param supplierId
	 * @param score  新评分
	 * @return
	 */
	public BigDecimal calcBuyerEvalScore(String supplierId,BigDecimal score) {
		List<TradeRating> tradeRatings = entityManager.createQuery(GET_SUPPLIER_TRADERATING,TradeRating.class)
										.setParameter("supplierId", supplierId).getResultList();
		BigDecimal totalScore = new BigDecimal(0);
		for (TradeRating tradeRating : tradeRatings) {
			totalScore = totalScore.add(new BigDecimal(tradeRating.getScore()));
		}
		//处理分值
		if(tradeRatings.size() > 0) {
			BigDecimal formerAverageScore = totalScore.divide(new BigDecimal(tradeRatings.size()),1,BigDecimal.ROUND_CEILING);
			totalScore = totalScore.add(score);
			BigDecimal averageScore = totalScore.divide(new BigDecimal(tradeRatings.size() + 1),1,BigDecimal.ROUND_CEILING);
			return averageScore.subtract(formerAverageScore);
		}else {
			return score;
		}
	}

	/**
	 * 更新评价分值
	 * @params supplierId
	 * @params updateScore  更新后的评分
	 * @return
	 */
	public BigDecimal updateBuyerEvalScore(String supplierId,BigDecimal formerScore ,BigDecimal updateScore) {
		List<TradeRating> tradeRatings = entityManager.createQuery(GET_SUPPLIER_TRADERATING,TradeRating.class)
				.setParameter("supplierId", supplierId).getResultList();
		BigDecimal totalScore = new BigDecimal(0);
		for (TradeRating tradeRating : tradeRatings) {
			totalScore = totalScore.add(new BigDecimal(tradeRating.getScore()));
		}
		//处理分值
		if(tradeRatings.size() > 0) {
			BigDecimal formerAverageScore = totalScore.divide(new BigDecimal(tradeRatings.size()),1,BigDecimal.ROUND_CEILING);
			BigDecimal currentAverageScore = (totalScore.subtract(formerScore).add(updateScore)).divide(new BigDecimal(tradeRatings.size()),1,BigDecimal.ROUND_CEILING);;
			return currentAverageScore.subtract(formerAverageScore);
		}else {
			return new BigDecimal(0);
		}
	}

	private static final String QUERY_PROFESSION_REVIEW_RECORD_EQL = ""
			+ "	SELECT			pro	"
			+ "	FROM			ProfessionRatingRecord pro"
			+ "	WHERE			pro.year = :year "
			+ "	AND				pro.supplier.id = :supplierId	";
	public BigDecimal calcProScore(String supplierId, int year) {
		Query query = entityManager.createQuery(QUERY_PROFESSION_REVIEW_RECORD_EQL);
		query.setParameter("year", year);
		query.setParameter("supplierId", supplierId);
		List<ProfessionRatingRecord> resultList = query.getResultList();
		if (resultList.size() == 0)
			return new BigDecimal(20);
		else 
			return new BigDecimal(resultList.get(0).getScore());
	}
	
	private static final String QUERY_ANNUAL_REVIEW_RECORD_EQL = ""
			+ "	SELECT			arr	"
			+ "	FROM			AnnualReviewRecord arr"
			+ "	WHERE			arr.year = :year "
			+ "	AND				arr.supplier.id = :supplierId	";
	public BigDecimal calcAnnualReviewScore(String supplierId, int year) {
		Query query = entityManager.createQuery(QUERY_ANNUAL_REVIEW_RECORD_EQL);
		query.setParameter("year", year);
		query.setParameter("supplierId", supplierId);
		List<AnnualReviewRecord> resultList = query.getResultList();
		if (resultList.size() == 0)
			return new BigDecimal(20);
		else 
			return resultList.get(0).getSumScore();
	}
	public static void main(String[] args) {
		System.out.println(new BigDecimal(20).divide(new BigDecimal(3),1,BigDecimal.ROUND_CEILING));
	}
}
